package comits.business.tiers.dao;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.context.ManagedSessionContext;

import comits.business.tiers.metiers.Depot;
import comits.business.tiers.metiers.Employe;
import comits.business.tiers.orm.hibernate.HibernateSession;
import comits.business.tiers.utilitaires.LoggingOutputStream;

public class EmployeDAOImpl implements EmployeDAO{
	
	public boolean connection(String login, String mdp,String Tble){
		
		
		Session session = HibernateSession.currentSession();
		SQLQuery sql;
		
		//System.out.println("empdao " + login + mdp + Tble);
		
		if(Tble.equalsIgnoreCase("BOUT")){
			sql =  session.createSQLQuery(PropertiesConfig.Get("selectMdpBtq"));
			//sql =  session.createSQLQuery("Select mdp from boutique where email=?");
			sql.setString(0,login);
			//System.out.println(mdp.equalsIgnoreCase((String) sql.uniqueResult()));
			if (mdp.equalsIgnoreCase((String) sql.uniqueResult())) return true;
		}
		if(Tble.equalsIgnoreCase("EMPL")){
			sql =  session.createSQLQuery(PropertiesConfig.Get("selectMdpEmp"));
			//sql =  session.createSQLQuery("Select mdp from employe where nom=?");
			sql.setString(0,login);
			//System.out.println(mdp.equalsIgnoreCase((String) sql.uniqueResult()));
			if (mdp.equalsIgnoreCase((String) sql.uniqueResult())) return true;
			
		}
		if(Tble.equalsIgnoreCase("DEPT")){
			sql =  session.createSQLQuery(PropertiesConfig.Get("selectMdpDpt"));
			//sql =  session.createSQLQuery("Select mdp from depot where email=?");
			sql.setString(0,login);
			//System.out.println(mdp.equalsIgnoreCase((String) sql.uniqueResult()));
			if (mdp.equalsIgnoreCase((String) sql.uniqueResult())) return true;
			
		}
		
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		
		return false;		
	}

	public String test() {
		// TODO Auto-generated method stub
		Session session = HibernateSession.currentSession();
		SQLQuery sql;
		String prop = PropertiesConfig.Get("selectEmp");
		sql =  session.createSQLQuery(prop);
		Object tuple = sql.uniqueResult();
		String nom = (String) tuple;
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		return nom;
	}

	public List<Employe> getListEmpMdp() {
		// TODO Auto-generated method stub getEmpMdp
		// TODO Auto-generated method stub
		Session session = HibernateSession.currentSession();
		SQLQuery sql;
		String prop = PropertiesConfig.Get("getEmpMdp");
		sql =  session.createSQLQuery(prop).addEntity(Employe.class);
		List<Employe> lEmp = sql.list();
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		return lEmp;
	}
	
	public List<Employe> getListEmpDpt() {
		// TODO Auto-generated method stub
		Session session = HibernateSession.currentSession();
		SQLQuery sql;
		String prop = PropertiesConfig.Get("getEmpDpt");
		sql =  session.createSQLQuery(prop).addEntity(Employe.class);
		List<Employe> lEmp = sql.list();
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		return lEmp;	
	}
	public List<Employe> getListEmploye(String site, Integer numSite) {
		// TODO Auto-generated method stub
		Session session = HibernateSession.currentSession();
		String prop = PropertiesConfig.Get("getEmpSite");
		SQLQuery sql =  session.createSQLQuery(prop).addEntity(Employe.class);
		sql.setString(0,site );
		sql.setInteger(1, numSite);
		List<Employe> lEmpSite = sql.list();
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		return lEmpSite;	
	}
	public String getNomEmploye(Integer numEmploye){
		SQLQuery sql =  HibernateSession.currentSession().createSQLQuery(PropertiesConfig.Get("getNomEmpoye"));
//		SQLQuery sql =  HibernateSession.currentSession().createSQLQuery("select Nom from employe where NumEmp=?");
		sql.setInteger(0, numEmploye);	
		String En =  (String) sql.uniqueResult();
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		return En;
	}
	public Integer getNumEmploye(String nomEmploye){
		SQLQuery sql =  HibernateSession.currentSession().createSQLQuery(PropertiesConfig.Get("getNumEmpoye"));
//		SQLQuery sql =  HibernateSession.currentSession().createSQLQuery("select NumEmp from employe where Nom=?");
		sql.setString(0, nomEmploye);	
		Integer En =  (Integer) sql.uniqueResult();
		ManagedSessionContext.unbind(HibernateSession.getSessionFactory());
		return En;
	}
}
